import {
    Getgroupon,
    Getnew,
    Getseckill
} from "../../http/api";

//初始值
const initState = {
    getnewlist: [],
    wanrenlist: [],
    seckilllist: []
}
//修改变量
const TYPES = {
    HOME_CHANGE_GETNEWLISST: "HOME_CHANGE_GETNEWLISST",
    HEME_CHANGE_WANRENLIST: "HEME_CHANGE_WANRENLIST",
    HOME_CHANGE_SECKILLLIST: "HOME_CHANGE_SECKILLLIST"
}
//异步
export const actions = {
    //新人专享
    changeGetnew: getnewlist => ({
        type: TYPES.HOME_CHANGE_GETNEWLISST,
        getnewlist
    }),
    reqGetnewAction: () => {
        return async (dispatch, getState) => {
            let res = await Getnew();
            if (res.data.code === 200) {
                dispatch(actions.changeGetnew(res.data.list))
            }
        }
    },
    //万人团
    changeWanrenlist: wanrenlist => ({
        type: TYPES.HEME_CHANGE_WANRENLIST,
        wanrenlist
    }),
    reqWanrenAction: () => {
        return async (dispatch, getState) => {
            let res = await Getgroupon();
            if (res.data.code === 200) {
                dispatch(actions.changeWanrenlist(res.data.list))
            }
        }
    },
    //秒杀
    changeSeckill:seckilllist=>({
        type:TYPES.HOME_CHANGE_SECKILLLIST,
        seckilllist
    }),
    reqSeckillAction:()=>{
        return async (dispatch,getState)=>{
            let res = await Getseckill();
            if(res.data.code===200){
                dispatch(actions.changeSeckill(res.data.list))
            }
        }
    }
}
//修改
export const homeReducer = (state = initState, action) => {
    switch (action.type) {
        case TYPES.HOME_CHANGE_GETNEWLISST:
            return {
                ...state,
                getnewlist: action.getnewlist
            }
        case TYPES.HEME_CHANGE_WANRENLIST:
            return{
                ...state,
                wanrenlist:action.wanrenlist
            }
        case TYPES.HOME_CHANGE_SECKILLLIST:
            return{
                ...state,
                seckilllist:action.seckilllist
            }
        default:
                return state;
    }
}

//导出数据
export const getNewlist = state => state.home.getnewlist
export const getWanrenlist = state => state.home.wanrenlist
export const getseckilllist =  state=>state.home.seckilllist